<?php

    session_start('studycheat_net');
    error_reporting(E_ALL ^ E_NOTICE);

    function callback($buffer) {
        global $_page_title;
        global $_page_description;

        $search = array('/%page-title%/',
                        '/%page-description%/'
                  );

        $replace = array($_page_title,
                         $_navigation[0]['title']
                   );

        return preg_replace($search, $replace, $buffer);
    }
  
    ob_start('callback');
  
    // includes
    require_once 'config.php';
    require_once 'connect.php';
    require_once 'functions.php';

    // path to particular page
    if (empty($_GET['page'])) {
        $_GET['page'] = 'home';
    }
  
    $path = "pages/";

    if (!empty($_GET['page'])) {
        $path         .= $_GET['page'] . '/';
        $_base_content = $path; 
      
        for ($i=0; $i<5; $i++) {
            if (!empty($_GET["subpage$i"])) {
                $path .= $_GET["subpage$i"] . '/';
            }
        }

        $_content = $path . 'index.php';
    }

    // navigation
    $_navigation = array();

    // page title
    if ('home' == $_GET['page']) {
        $title = 'StudyCheat - %page-title%';
    } else {
        $title = '%page-title% - StudyCheat &bull; ';
    }

    // common page description
    $_page_description = 'cooperative studying, easily';

    // is logged in
    if (isset($_GET['logged'])) {
        $info_box = 'user_logged';
    } elseif (isset($_GET['not-logged'])) {
        $info_box = 'user_not_logged';
    }
    
    // logout
    if (isset($_GET['logout'])) {
        $_SESSION['user'] = null; 
        unset($_SESSION['user']);
    
        if (@$_SERVER['HTTP_REFERER'] != '') {
            header("location: {$_SERVER['HTTP_REFERER']}");
        } else {
            header("location: /");
        }
        
        exit();
    }

    // login
    if (isset($_POST['login-submit'])) {
        $email_save  = mysql_escape_string($_POST["email"]);
        $passwd_hash = md5($_POST["password"]);
        $res         = mysql_query("SELECT * FROM student WHERE email='{$email_save}' AND password='{$passwd_hash}' LIMIT 1");
    
        if (mysql_num_rows($res) == 1) {
            $row = mysql_fetch_array($res);
            
            $_SESSION['user']['auth'] = true;
            $_SESSION['user']['id']   = $row['id'];
            $_SESSION['user']['meno'] = $row['name'] . ' ' . $row['surname'];

            // last login update
            mysql_query("UPDATE INTO log (NOW(), '{$_SESSION['uzivatel']['id']}', 'user logged in'");

            // back to referer
            if (@$_SERVER['HTTP_REFERER'] != '') {
                header("location: {$_SERVER['HTTP_REFERER']}");
            } else {
                header("location: /");
            }

            exit('Login failed.');
        } else {
            $info_box = 'login_error';
        }
    } 
  
 ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title><?php echo $title; ?></title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv="Content-Language" content="en" />
        <meta name="Description" content="%page-description%" />
        <meta name="Robots" content="all,follow" />
    
        <link href="/css/screen.css" rel="stylesheet" type="text/css" media="all" />
        <?php

            // subpage CSS
            if (is_readable("$path/screen.css")) {
                echo "<link href='/{$path}screen.css' rel='stylesheet' type='text/css' media='all' />\n";
            }

         ?>
        <link href="/css/print.css" rel="stylesheet" type="text/css" media="print" /> 
        <link rel="bookmark" href="http://www.studycheat.net/" title="StudyCheat - cooperative studying, easily" />

        <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<?php /*        <script type="text/javascript" src="/js/markermanager.js"></script> */ ?>             
        <script type="text/javascript" src="/js/jquery.js"></script>
        <?php                                                   

            // subpage JavaScript
            if (is_readable("$path/script.js")) {
                echo "<script type='text/javascript' src='/{$path}script.js'></script>\n";
            }

         ?>
    </head>

    <body id="studycheat-net">    
        <header>
            <h1 class="hidden-text">
                <a title="StudyCheat&nbsp;&ndash; cooperative studying, easily">StudyCheat&nbsp;&ndash; cooperative studying, easily</a>
            </h1>
            
            <nav>
            	<ul>
            		<li><a href="/" title="Homepage">Homepage</a></li>
            		<li><a href="/documents" title="Documents">Documents</a></li>
            		<li><a href="/course-detail" title="Courses">Courses</a></li>
            	</ul>
            </nav>
        </header>


        <!-- main column -->
        <section id="main-col">
            <?php
            
                if (is_readable($_content)) {
                    require_once $_content;
                } else {
                    header('HTTP/1.0 404 Not Found');
                    header("location: /404/");
                    
                    exit('Bad URL.');
                }
    
            ?>
        </section>

        <!-- side column -->
        <section id="side-col">
            <h3>List of schools</h3>
            
        	<nav>
        		<ul>
            		<li><a href="#">Masaryk university</a></li>
                    <li><a href="#">Carl university</a></li>
                    <li><a href="#">Harvard university</a></li>
                    <li><a href="#">Oxford university</a></li>
                    <li><a href="#">Moscow university</a></li>
        		</ul>
        	</nav>
        </section>



        <aside id="login">
            <hgroup>
        		<h3>Login</h3>
        	</hgroup>
        	
            <p>
                Ac magna dolor eu enim parturient pulvinar, natoque dolor nunc. Ac a nascetur sagittis placerat turpis! 
                Turpis, ridiculus mus in! Porttitor ut natoque magna odio nisi! Ultrices urna, etiam turpis nec urna?
            </p>
        </aside>
     
        <!-- navigation -->
        <aside id="page-navigation">
            <h5 class="non-visible">You are here:</h5>
    
            <ul>
            <?php

                if ($_GET['page'] == 'home') {
                    echo "<li>Homepage</li>\n";
                } else {
                    echo "<li><a href='/'>Homepage</a></li>\n";
                }

                // generujem navigaciu a titulok stranky
                if (is_array($_navigation)) {
                    $i    = 0;
                    $link = '/';

                    foreach ($_navigation as $value) {
                        $i++;

                        if (('' != $value['link']) && ($i != count($_navigation))) {
                  
                            if ($value['link']{0} == '/') {
                                $link = $value['link']; 
                            } else {
                                $link .= $value['link'];
                            }
                  
                            $_page_title = ' - ' . $value['title'] . $_page_title;
                  
                            echo "<li><a href='$link'>{$value['title']}</a></li>\n";
                        } else {
                            $_page_title = $value['title'] . $_page_title;
                  
                            echo "<li>{$value['title']}</li>\n";
                        }
                    }
                }

             ?>
            </ul>
        </aside>
        
        <footer>
            <p>Copyright &copy; 2010 by 80ML Creative group, licensed under GPL3</p>
        </footer>
    </body>
</html>

<?php

    if (!empty($info_box)) {
        switch ($info_box) {
            case 'registration_ok':     $tmp = 'You have been sucessfully registred. Thanks.'; break; 
            case 'registration_error':  $tmp = 'Sending your registration failed. Please check entered data.'; break; 
            
            case 'settings_ok':         $tmp = 'Your settings have been successfully sent.'; break; 
            case 'settings_error':      $tmp = 'Sending your settings failed. Please check entered data.'; break; 
            
            case 'login_error':         $tmp = 'Login failed, Please check entered data.'; break;
        }
      
        echo "<script type='text/javascript'>alert('$tmp')</script>";
    } 
 
    ob_end_flush();
 
?>
     